package com.eqdd.yiqidian.ui.companyspace

import android.content.Intent
import android.view.View
import android.widget.RelativeLayout
import com.eqdd.yiqidian.R
import com.eqdd.yiqidian.adapter.slimadapter.SlimInjector
import com.eqdd.yiqidian.adapter.slimadapter.viewinjector.IViewInjector
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimEditEnterBean
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimImageSelectBean
import com.eqdd.yiqidian.http.HttpConfig
import com.eqdd.yiqidian.kotlin.BaseAddActivity
import com.eqdd.yiqidian.kotlin.inflater
import com.eqdd.yiqidian.kotlin.localMediaImgPreview
import com.eqdd.yiqidian.kotlin.toArrayList
import com.eqdd.yiqidian.utils.ImageUtil
import com.eqdd.yiqidian.utils.PicUtil
import com.luck.picture.lib.PictureSelector
import com.luck.picture.lib.config.PictureConfig
import com.luck.picture.lib.entity.LocalMedia
import com.lzy.okgo.model.HttpParams
import kotlinx.android.synthetic.main.layout_image_grid_item.view.*
import kotlinx.android.synthetic.main.layout_image_selected.*
import org.jetbrains.anko.sdk25.coroutines.onClick
import java.io.File

/**
 * Created by jhj on 18-6-28.
 */
class EquipmentAddActivity : BaseAddActivity() {

    var multiSelect: ArrayList<LocalMedia>? = null
    var existImageCount = 0

    override val title: String
        get() = "企业设备"
    override val url: String
        get() = HttpConfig.BASE_URL + HttpConfig.EQUIPMENT_ADD

    override fun uploadParams(): HttpParams {
        val params = HttpParams()
        val data = slimAdapterEx.data
        val list = data[1].tag as? List<LocalMedia>
        val fileList = list?.map {
            File(it.compressPath ?: it.path)
        }
        params.put("EquipmentName", data[0].content)
        params.putFileParams("images", fileList)
        params.put("Manufactor", data[2].content)
        params.put("DateOfPurchase", data[3].content)
        params.put("EquipmentMsg", data[4].content)
        return params
    }

    override fun itemClick(p: Int) {

    }

    override fun setEditBeans() {
        datas.add(SlimEditEnterBean("设备名称", "", 0, "请输入").setNeed(true))
        datas.add(SlimImageSelectBean("图       片", 1, "").setDeal(true).setNeed(true))
        datas.add(SlimEditEnterBean("设备厂商", "", 2, "请输入").setNeed(true))
        datas.add(SlimEditEnterBean("生产日期", "", 3, "请输入").setNeed(true))
        datas.add(SlimEditEnterBean("详细描述", "", 4, "请输入").setNeed(true))
        slimAdapterEx.register<SlimImageSelectBean>(R.layout.layout_image_selected, object : SlimInjector<SlimImageSelectBean> {
            override fun onInject(data: SlimImageSelectBean?, injector: IViewInjector<out IViewInjector<*>>?) {
                injector?.text(R.id.tv_label, data?.title)
                        ?.clicked(R.id.iv_add) {
                            PicUtil.multi(this@EquipmentAddActivity, multiSelect)
                        }
            }
        })
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == RESULT_OK && requestCode == PictureConfig.CHOOSE_REQUEST) {
            val localMedias = PictureSelector.obtainMultipleResult(data)
            if (existImageCount != 0) {
                linearLayout.removeViews(1, existImageCount)
            }
            multiSelect = localMedias.toArrayList()
            localMedias?.forEachIndexed { index, localMedia ->
                val density = resources.displayMetrics.density
                val params = RelativeLayout.LayoutParams(linearLayout.height, linearLayout.height)
                params.setMargins(0, 0, 5 * density.toInt(), 0)
                val view = inflater.inflate(R.layout.layout_image_grid_item, null)
                view.iv_picture.layoutParams = params
                ImageUtil.setImage(localMedia.compressPath, view.iv_picture)
                view.iv_picture.onClick {
                    localMediaImgPreview(localMedias, index)
                }
                view.iv_delete.visibility = View.VISIBLE
                view.iv_delete.onClick {
                    existImageCount--
                    multiSelect?.remove(localMedia)
                    linearLayout.removeView(view)
                }
                linearLayout.addView(view)
            }
            existImageCount = localMedias.size
            slimAdapterEx.getDataItem(1).tag = localMedias
        }
    }
}